package woa.binarysearch;

/**
 * 寻找比目标字母大的最小字母
 *
 * @author wangpeng
 * @date 2021/12/21
 */
public class NextGreatestLetter {

    public static char nextGreatestLetter(char[] letters, char target) {
        int left = 0, right = letters.length - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (letters[mid] <= target) {
                left = mid + 1;
            } else {
                if (mid > 0 && letters[mid - 1] <= target) {
                    return letters[mid];
                }
                right = mid - 1;
            }
        }
        return letters[0];
    }

    public static void main(String[] args) {
        System.out.println(nextGreatestLetter(new char[]{'a','b','e','j','k'}, 'e'));
    }
}
